package com.yunbao.hujin.zqqgetuidemo.service;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;

import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.yunbao.hujin.zqqgetuidemo.MainActivity;

/**
 * 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>
 * onReceiveMessageData 处理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理回执 <br>
 */

public class DemoIntentService extends GTIntentService {

    /**
     * 1. 接收 ServicePid
     */
    @Override
    public void onReceiveServicePid(Context context, int i) {
        Log.e(TAG, "onReceiveServicePid -> " + "ServicePid = " + i);
    }

    /**
     * 2. 接收 cid
     */
    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);
        sendMessage(clientid, 1);
    }

    /**
     * 3. 处理透传消息
     */
    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
//        Log.e(TAG, "onReceiveMessageData -> " + "gtTransmitMessage = " + msg.getMessageId());
//        Log.e(TAG, "onReceiveMessageData2 -> " + "gtTransmitMessage = " + msg.getPayload().toString());//地址
        String taskid = msg.getTaskId();
        String messageid = msg.getMessageId();

        // 第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行
        boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
        Log.d(TAG, "call sendFeedbackMessage = " + (result ? "success" : "failed"));

        byte[] payLoad = msg.getPayload();
        if (payLoad != null) {
            String data = new String(payLoad);
            Log.e("Tag--DemoIntentService", data);

            //第一种方式--通过Handler发送数据
//            sendMessage(data, 0);

            //第二种方式--通过广播发送数据
            Intent intent = new Intent(MainActivity.MY_BROADCAST);
            intent.putExtra("data", data);
//            LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            sendBroadcast(intent);
        }
    }

    /**
     * 4. cid 离线上线通知
     */
    @Override
    public void onReceiveOnlineState(Context context, boolean b) {
        Log.e(TAG, "onReceiveOnlineState -> " + "boolean = " + b);
    }

    /**
     * 5. 各种事件处理回执(比如有标签的消息)
     */
    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {
        Log.e(TAG, "onReceiveCommandResult -> " + "gtCmdMessage = " + gtCmdMessage.getPkgName());
    }

    private void sendMessage(String data, int what) {
        Message message = Message.obtain();
        message.what = what;
        message.obj = data;
        MainActivity.sendMessage(message);
    }

}
